package cn.tedu.jdbc;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/*
Author     :Dsi
Date       : 2021/11/30 15:24
Description: 该类用于测试JDBC的入门案例
*/
public class Test01 {
    public static void main(String[] args) throws Exception {
        /*1.找到jdbc的驱动类*/
        Class.forName("com.mysql.cj.jdbc.Driver");
        /*2.取得与数据库的连接
         *格式：协议：//IP地址：端口/数据库名字*/
        String url = "jdbc:mysql://localhost:3307/cgb1129";
        Connection c = DriverManager.getConnection(url, "root", "root");
        /*3.获取传输器*/
        Statement s = c.createStatement();
        /*4.执行sql，查询所有用户*/
        String sql = "select * from students";
        ResultSet sql1 = s.executeQuery(sql);
        /*5.解析结果集*/
        for (int j = 0; sql1.next(); j++) {
            for (int i = 1; i <= 5; i++) {
                Object object = sql1.getObject(i);
                System.out.print(object+" ");
                if(i==5)break;
            }
            System.out.println();
        }
        /*6.关闭资源*/
        sql1.close();
        c.close();
        s.close();
    }
}



















